package com.simtop.picture.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simtop.picture.common.ServerResponse;
import com.simtop.picture.mapper.PictureMapper;
import com.simtop.picture.pojo.*;
import com.simtop.picture.service.PictureService;
import com.simtop.picture.utils.EmptyUtil;
import com.simtop.picture.utils.PropertiesUtils;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PictureServiceImpl implements PictureService {

    @Autowired
    public PictureMapper pictureMapper;

    private static String netPath= PropertiesUtils.getNetProperties();

    @Override
    public ServerResponse<List<ExplorationCenter>> getCenter() {
        List<ExplorationCenter> explorationCenterList=pictureMapper.getCenter();
        return ServerResponse.createBySuccess(explorationCenterList);
    }

    @Override
    public ServerResponse<List<PrimaryTitle>> getPrimaryTitle(int centerId) {
        List<PrimaryTitle> primaryList=pictureMapper.getPrimaryTitle(centerId);
        return ServerResponse.createBySuccess(primaryList);
    }

    @Override
    public ServerResponse<List<SecondaryTitle>> getSecondTitle(int primaryId) {
        List<SecondaryTitle> secondaryTitleList=pictureMapper.getSecondTitle(primaryId);
        return ServerResponse.createBySuccess(secondaryTitleList);
    }

    @Override
    public ServerResponse addPrimaryTitle(Integer explorationCenterId, String primaryTitleName) {
        int count=0;
        PrimaryTitle primaryTitle=pictureMapper.checkPrimary(primaryTitleName);
        if(EmptyUtil.isNotEmpty(primaryTitle)){
            return ServerResponse.createByErrorMsg("该一级标签名已经存在");
        }else{
            count=pictureMapper.addPrimaryTitle(explorationCenterId,primaryTitleName);
        }
        if(count!=0){
            return ServerResponse.createBySuccessMsg("成功添加一级标签名");
        }else{
            return ServerResponse.createBySuccessMsg("添加一级标签名失败");
        }
    }

    @Override
    public ServerResponse addSecondTitle(Integer primaryTitleId, String secondaryTitleName) {
        int count=0;
        SecondaryTitle secondaryTitle=pictureMapper.checkSecond(secondaryTitleName);
       if(EmptyUtil.isNotEmpty(secondaryTitle)){
           return ServerResponse.createByErrorMsg("该二级标签名已经存在");
       }else{
           count=pictureMapper.addSecondTitle(primaryTitleId,secondaryTitleName);
       }
        if(count!=0){
            return ServerResponse.createBySuccessMsg("成功添加二级标签名");
        }else{
            return ServerResponse.createBySuccessMsg("添加二级标签名失败");
        }
    }


    @Override
    @Transactional
    public ServerResponse deletePrimaryTitle(Integer primaryTitleId) {
        pictureMapper.deletePicturePathByPrimary(primaryTitleId);
        int secondCount=pictureMapper.deleteSecondByPrimary(primaryTitleId);
        int primaryCount=pictureMapper.deletePrimaryTitle(primaryTitleId);
        if(primaryCount!=0){
            return ServerResponse.createBySuccessMsg("成功删除一级标签");
        }else{
            return  ServerResponse.createByErrorMsg("删除一级标签失败");
        }
    }

    @Override
    public ServerResponse deleteSecondTitle(Integer secondaryTitleId) {
        int second=pictureMapper.deleteSecondTitle(secondaryTitleId);
        if(second!=0){
            return ServerResponse.createBySuccessMsg("成功删除二级标签");
        }else{
            return ServerResponse.createByErrorMsg("删除二级标签失败");
        }
    }

    @Override
    public int addPicture(PicturePath picturePath) {
        return pictureMapper.addPicture(picturePath);
    }

    @Override
    public ServerResponse<PageInfo> getImage(Integer pageNum, Integer pageSize, Integer secondaryTitleId) {
        PageHelper.startPage(pageNum,pageSize);
        List<PicturePath> picturePaths=pictureMapper.getImage(secondaryTitleId);
        PageInfo<PicturePath> picturePathPageInfo=new PageInfo<>(picturePaths);
        return ServerResponse.createBySuccess(picturePathPageInfo);
    }


    @Override
    public ServerResponse<String> updatePrimaryTitle(Integer primaryTitleId, String primaryTitleName) {
        PrimaryTitle primaryTitle=pictureMapper.checkPrimary(primaryTitleName);
        if(EmptyUtil.isNotEmpty(primaryTitle)){
            return ServerResponse.createByErrorMsg("该一级标签名已存在");
        }
        int updatePrimary=pictureMapper.updatePrimaryTitle(primaryTitleId,primaryTitleName);
        if(updatePrimary!=0){
            return ServerResponse.createBySuccessMsg("修改成功一级标签名");
        }else{
            return ServerResponse.createByErrorMsg("一级标签名修改失败");
        }
    }

    @Override
    public ServerResponse<String> updateSecondTitle(Integer secondaryTitleId, String secondaryTitleName) {
        SecondaryTitle secondaryTitle=pictureMapper.checkSecond(secondaryTitleName);
        if(EmptyUtil.isNotEmpty(secondaryTitle)){
            return ServerResponse.createByErrorMsg("该二级标签名已经存在");
        }
        int updateSecond=pictureMapper.updateSecondTitle(secondaryTitleId,secondaryTitleName);
        if(updateSecond!=0){
            return ServerResponse.createBySuccessMsg("修改成功二级标签名");
        }else{
            return ServerResponse.createByErrorMsg("二级标签名修改失败");
        }
    }

    @Override
    public ServerResponse<PageInfo> getPicture(Integer pageNum, Integer pageSize, Integer explorationCenterId) {
        if(StringUtils.isEmpty(pageNum)){
            pageNum=1;
        }
        if(StringUtils.isEmpty(pageSize)){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<PicturePath> picturePaths=pictureMapper.getPicture(explorationCenterId);
        picturePaths.forEach(picturePath -> {
//            System.out.println("图片名称"+picturePath.getPictureName());
            String imagesPath=picturePath.getPicturePath();
            String targetPath=picturePath.getPictureTargetPath();
            picturePath.setPicturePath(netPath+imagesPath.substring(imagesPath.lastIndexOf("\\")+1));
            picturePath.setPictureTargetPath(netPath+targetPath.substring(targetPath.lastIndexOf("\\\\")+2));
        });
        PageInfo<PicturePath> picturePathPageInfo=new PageInfo<>(picturePaths);
        return ServerResponse.createBySuccess(picturePathPageInfo);
    }

    @Override
    public ServerResponse<String> deletePicturePath(Integer picturePathId) {
        int deletePath=pictureMapper.deletePicturePath(picturePathId);
        if(deletePath!=0){
            return ServerResponse.createBySuccessMsg("成功删除该图片资源");
        }else{
            return ServerResponse.createByErrorMsg("删除该图片资源失败");
        }
    }

    @Override
    public int compilePicturePath(PicturePath picturePath) {
        int compilePicturePath=pictureMapper.compilePicturePath(picturePath);
        return compilePicturePath;
    }

    @Override
    public ServerResponse<PageInfo> filterPicture(Integer pageNum, Integer pageSize, Integer secondId) {
        if(StringUtils.isEmpty(pageNum)){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<PicturePath> picturePaths=pictureMapper.filterPicture(secondId);
        picturePaths.forEach(picturePath -> {
            String imagesPath=picturePath.getPicturePath();
            String targetPath=picturePath.getPictureTargetPath();
            picturePath.setPicturePath(netPath+imagesPath.substring(imagesPath.lastIndexOf("\\")+1));
            picturePath.setPictureTargetPath(netPath+targetPath.substring(targetPath.lastIndexOf("\\\\")+2));
        });
        PageInfo<PicturePath> picturePathPageInfo=new PageInfo<>(picturePaths);
        return ServerResponse.createBySuccess(picturePathPageInfo);
    }


    @Override
    public SecondaryTitle getSecondaryTitleName(Integer secondaryTitleId) {
        return pictureMapper.getSecondaryTitleName(secondaryTitleId);
    }

    @Override
    public List<SecondaryTitle> create() {
        return pictureMapper.create();
    }


    @Override
    public ServerResponse getPictureDetail(Integer picturePathId) {
        PicturePath picturePath=pictureMapper.getPictureDetail(picturePathId);
        System.out.println("数据"+picturePath);
        String imagesPath=picturePath.getPicturePath();
        String targetPath=picturePath.getPictureTargetPath();
        picturePath.setPicturePath(netPath+imagesPath.substring(imagesPath.lastIndexOf("\\")+1));
        picturePath.setPictureTargetPath(netPath+targetPath.substring(targetPath.lastIndexOf("\\\\")+2));
        return  ServerResponse.createBySuccess(picturePath);
    }


    @Override
    public List<U3dPath> getU3dPath() {
        List<U3dPath> u3dPathList=pictureMapper.getU3dPath();
        u3dPathList.forEach(u3dPath -> {
            List<PicturePath> picturePathList=u3dPath.getPicturePathList();
            picturePathList.forEach(picturePath -> {
                String imagesPath=picturePath.getPicturePath();
                String targetPath=picturePath.getPictureTargetPath();
                picturePath.setPicturePath(netPath+imagesPath.substring(imagesPath.lastIndexOf("\\")+1));
                picturePath.setPictureTargetPath(netPath+targetPath.substring(targetPath.lastIndexOf("\\\\")+2));
            });
        });
        return u3dPathList;
    }


    @Override
    public void deleteUser() {
        pictureMapper.deleteUser();
    }

    @Override
    public long checkPictureName(String pictureName,Integer picturePathId) {
        return pictureMapper.checkPictureName(pictureName,picturePathId);
    }

    @Override
    public long checkAddPictureName(String pictureName,String imagesPath) {
        return pictureMapper.checkAddPictureName(pictureName,imagesPath);
    }


    @Override
    public long checkImagePath(String imagesPath, Integer picturePathId) {
        return pictureMapper.checkImagePath(imagesPath,picturePathId);
    }
}
